package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Style;
import cn.edu.sig.bean.User;
import cn.edu.sig.dao.UserDao;
import cn.edu.sig.util.PaginationHelper;

@Repository("userDao")
public class UserDaoImpl extends TemplateSupport implements UserDao {

	public void add(User t) {

		String sql = this.sqlSource.getSql("user.add");

		Object[] param = new Object[] { t.getCode(), t.getEmail(),
				t.getUserName(), t.getPassword() };

		this.getJdbcTemplate().update(sql, param);

	}

	public void delete(Integer id) {
		
		

	}

	public void edit(User t) {
		String sql= this.sqlSource.getSql("user.update");
				
				Object[] param = new Object[] {
					t.getUserName(),t.getSex(),
					t.getGrade(),t.getPhone(),
					t.getAddrezz(),t.getEmail(),t.getCode()};
				
		this.getJdbcTemplate().update(sql, param);		
	}

	public List<User> findAll() {
		String sql = this.sqlSource.getSql("user.list");

		RowMapper<User> mapper = new RowMapper<User>() {
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				user.setUserName(rs.getString("userName"));
				user.setEnable(rs.getBoolean("enable"));
				user.setEmail(rs.getString("email"));
				user.setGrade(rs.getString("grade"));

				return user;
			}
		};

		return this.getJdbcTemplate().query(sql, mapper);
	}

	public User findById(String id) {

		String sql = this.sqlSource.getSql("user.login");

		RowMapper<User> userMapper = new RowMapper<User>() {
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				user.setUserName(rs.getString("userName"));
				user.setEmail(rs.getString("email"));
				user.setPassword(rs.getString("password"));
				user.setAddrezz(rs.getString("addrezz"));
				user.setGrade(rs.getString("grade"));
				user.setSex(rs.getInt("sex"));
				user.setPhone(rs.getString("phone"));

				return user;
			}
		};

		return (User)this.getJdbcTemplate().queryForObject(sql, new Object[] { id },
				userMapper);
	}

	public User findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public void register(User user) {
		String sql = this.sqlSource.getSql("user.register");

		Object[] param = new Object[] { user.getCode(), user.getSex(),
				user.getUserName(), user.getPassword() };

		this.getJdbcTemplate().update(sql, param);

	}

	public void updatePassword(String code,String password) {
		String sql = this.sqlSource.getSql("user.update.password");
		
		Object[] param = new Object[] {password,code};
		
		this.getJdbcTemplate().update(sql, param);
		
	}

	public CurrentPage<User> findUserList(int pageNo,int pageSize) {
		PaginationHelper<User> ph = new PaginationHelper<User>();
        return ph.fetchPage(
        		this.getJdbcTemplate(),
                "SELECT count(*) FROM user",
                "SELECT * FROM user Limit ? ,?",
                "",
                pageNo,
                pageSize,
                new ParameterizedRowMapper<User>() {
                    public User mapRow(ResultSet rs, int i) throws SQLException {
                    	User user = new User();
        				user.setCode(rs.getString("code"));
        				user.setUserName(rs.getString("userName"));
        				user.setEmail(rs.getString("email"));
        				user.setPassword(rs.getString("password"));
        				user.setAddrezz(rs.getString("addrezz"));
        				user.setGrade(rs.getString("grade"));
        				user.setSex(rs.getInt("sex"));
        				user.setPhone(rs.getString("phone"));

        				return user;
                    	
                    }
                }
        );
	}

}
